#include <QApplication>

#include "DGVMainVTK.h"

///Function Prototypes
void processArgs(int argcount, char *argvars[], QString &filename, int &interval, bool &verbose, bool &scale);

int main(int argc, char *argv[])
{
	QApplication app(argc,argv);
    bool verboseMode = false, scaling = false;
    QString name;

    cerr << ">| Surface Plot Animator" << endl;
    cerr << ">| Copyright Shekhar S. Chandra, 2008" << endl;

    //--------------------------------------------
    ///Process arguments
    int interval = 1;
	processArgs(argc,argv,name,interval,verboseMode,scaling);

	//--------------------------------------------
    ///Load Image
    DGVMainVTK MainWindow;

    ///Open Image
    MainWindow.show();
    qApp->processEvents();
    if(!MainWindow.loadFile(name))
    {
        cerr << ">| ERROR Loading File: " << name.toStdString() << endl;
        MainWindow.done();
        return app.exec();
    }
    DGVImageVTK *image = MainWindow.activeImage();

    DGVSurfacePlotVTK *splot = new DGVSurfacePlotVTK(&MainWindow);

    splot->setConsole(MainWindow.getConsole());
    qApp->processEvents();
    if(image->isVolume())
    {
        MainWindow.getConsole()->printWarning("Valid volume found, so animating surface plot.");

        MainWindow.working();
        //--------------------------------------------
        ///Extract Volume
        DGVVolumePlotVTK *volume = image->getVolume();

        splot->setName("Surface Animation");
        if(scaling)
            splot->autoScaleSurface(true);
        splot->animateFromSlices(&volume->data(),interval);
        MainWindow.displaySurfacePlot(splot);
        MainWindow.done();
    }
    else
    {
        MainWindow.getConsole()->printWarning("Not a volume, so not animating.");

        MainWindow.working();
        splot->setName("Surface Plot");
        splot->SetInput(image->GetOutput());
        splot->generatePlot();
        splot->generateAxes();
        MainWindow.displaySurfacePlot(splot);
        MainWindow.done();
    }
    //--------------------------------------------

    return app.exec();
}


void processArgs(int argcount, char *argvars[], QString &filename, int &interval, bool &verbose, bool &scale)
{
    QString tmpStr;

    if(argcount == 3)
    {
        filename = argvars[1];
        tmpStr = argvars[2];
        interval = tmpStr.toInt();
        tmpStr = "";
    }
    else if(argcount > 3)
    {
        filename = argvars[1];
        tmpStr = argvars[2];
        interval = tmpStr.toInt();
        tmpStr = "";
        for(int j = 3; j < argcount; j++)
            tmpStr += argvars[j];
        if(tmpStr.contains("--verbose",Qt::CaseInsensitive))
            verbose = true;
        if(tmpStr.contains("--scale",Qt::CaseInsensitive))
            scale = true;
    }
    else
    {
        printf(">| Usage: %s <Image> <Interval (ms)> [--verbose] [--scale]\n",argvars[0]);
        printf(">| Loads a 3D image stack and displays animated Surface plot at an\n");
        printf(">| Interval provided in milliseconds (ms).\n");
        printf(">| [--verbose] argument is optional and outputs all posible data.\n");
        printf(">| [--scale] argument is optional and enables scaling of the surface always.\n");
        exit(EXIT_FAILURE);
    }
}
